﻿using ProgrammingEducationSystem.DbConfig;
using ProgrammingEducationSystem.Models;
using Microsoft.EntityFrameworkCore;

namespace ProgrammingEducationSystem.Repository.IRepository
{
    public class CourseRepository : BaseRepository<Course>, ICourseRepository
    {
        public CourseRepository(AppDbCtx appDbCtx) : base(appDbCtx)
        {
        }

        public async Task<List<Course>> GetByClassIdAsync(Guid classId)
        {
            return await _appDbCtx.Courses.Where(c => c.ClassId == classId).ToListAsync();
        }
        public async Task<List<Course>> FindByTeacherIdAsync(string teacherId)
        {
            var cla = await _appDbCtx.Classes.Where(c => c.TeacherId == teacherId).ToListAsync();
            return await _appDbCtx.Courses.Where(cr => cla.Any(sa => sa.Id == cr.ClassId)).ToListAsync();
        }

        public async Task<List<Course>> FindByStudentIdAsync(string studentId)
        {
            var stu = await _appDbCtx.StuAndClasses.Where(c => c.StudentId == studentId).ToListAsync();
            var cls = await _appDbCtx.Classes.Where(s => stu.Any(r => r.ClassId == s.Id)).ToListAsync();
            return await _appDbCtx.Courses.Where(a => cls.Any(b => b.Id == a.ClassId)).ToListAsync();
        }

        public async Task<List<Course>> GetByClassTypeAsync(Guid classTypeId)
        {
            var clt = await _appDbCtx.CourseTypes.Where(a => a.Id == classTypeId).ToListAsync();
            return await _appDbCtx.Courses.Where(b => clt.Any(c => c.Id == b.CourseTypeId)).ToListAsync();
        }

        public async Task<List<Course>> GetByClassTypeNameAsync(string classTypeName)
        {
           var cln  = await _appDbCtx.CourseTypes.Where(a => a.Name == classTypeName).ToListAsync();
            return await _appDbCtx.Courses.Where(b => cln.Any(c => c.Id == b.CourseTypeId)).ToListAsync();
        }

     
    }
}
